package com.edu.oss.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.edu.oss.exception.GuliException;
import com.edu.oss.service.OssService;
import com.edu.oss.utils.ConstantPropertiesUtils;
import com.edu.oss.utils.InitVodClient;
import com.edu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * oss文件上传的接口
 *
 * @author mxs
 */
@RestController
@CrossOrigin
@RequestMapping("ali/oss/fileoss")

public class OssController {
    @Autowired
    private OssService ossService;

    /**
     * 上传头像
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R uploadOssFile(MultipartFile file) {
        String url = ossService.uploadFileAvatar(file);
        return R.success().data("url", url);
    }

    /**
     * 上传视频
     *
     * @param file 上传的视频文件
     * @return 返回视频播放凭证
     */
    @PostMapping("/uploadVideo")
    public R uploadOssVideo(MultipartFile file) {
        String videoPath = ossService.uploadVideo(file);
        return R.success().data("url", videoPath);
    }

    /**
     * 删除在阿里云的点播视频
     *
     * @param id 根据视频id删除视频
     * @return
     */
    @DeleteMapping("/removeAlyVideo/{id}")
    public R removeAlyVideo(@PathVariable String id) {
        DefaultAcsClient client = InitVodClient.initVodClient(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET);
        //创建删除对象
        DeleteVideoRequest request = new DeleteVideoRequest();
        request.setVideoIds(id);
        try {
            client.getAcsResponse(request);
            return R.success();
        } catch (Exception e) {
            //throw new GuliException(20001, "视频删除失败");
            return R.error();
        }
    }

    /**
     * 删除列表的视频
     *
     * @param videoList
     * @return
     */
    @DeleteMapping("/removeVideo")
    public R removeVideos(@RequestParam(value = "videoList") List<String> videoList) {
        ossService.removeVideos(videoList);
        return R.success();
    }

    /**
     * 获取视频id获取视频播放凭证
     *
     * @param id
     * @return
     */
    @GetMapping("/getPlayAuth/{id}")
    public R getAuth(@PathVariable String id) {

        //创建初始化对象
        DefaultAcsClient client = InitVodClient.initVodClient(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET);
        //创建获取凭证request和response对象
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        //在request中设置视频id
        request.setVideoId(id);
        //调用方法获取凭证
        GetVideoPlayAuthResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        String playAuth = response.getPlayAuth();
        return R.success().data("playAuth", playAuth);


    }
}
